<?php

namespace App\Http\Repositories\StoreCenter;

use App\Http\Repositories\BaseRepository;
use App\Models\MongoDb\Models\OrderMongoDb;
use App\Models\StoreCenter\Models\CartModel;

class CartRepository extends BaseRepository
{
    public $cart;
    public function __construct(CartModel $cart)
    {
        $this->cart = $cart;
    }

    public function getCartInfo($data)
    {
        $where['id'] = $data['id'];
        $where['create_user_uuid'] = $this->cart->getCurrentUserId();

        return $this->cart->getFirst($where);
    }

    public function getCartList($data)
    {
        is_real_exists(@$data['create_user_uuid']) && $where['create_user_uuid'] = $data['create_user_uuid'];
        is_real_exists(@$data['spu']) && $where['spu'] = $data['spu'];
        is_real_exists(@$data['sku']) && $where['sku'] = $data['sku'];
        is_real_exists(@$data['create_user']) && $where['create_user'] = ['like', '%'.$data['create_user'].'%'];
        is_real_exists(@$data['create_time']) && $where['create_time'] = ['between', $data['create_time']];

        return $this->cart->getCartList($where ?? []);

    }

    public function addCart($data)
    {
        $cart = $this->cart->addItem($data, true);

        return [];
    }

    public function delCart($data)
    {
        $id = explode(',', $data['ids']);
        $success = 0;
        $fail = 0;
        $message = '';
        foreach ($id as $k => $v) {
            $where['id'] = $v;
            $where['create_user_uuid'] = $this->cart->getCurrentUserId();
            $model = $this->cart->handleCondition($where)->first();
            if (empty($model)) {
                $fail ++;
                $message .= '第'.($k+1).'个删除失败的原因:数据不存在;';
            } else {
                $success ++;
                $model->delete();
            }
        }

        return ['message' => '成功删除'.$success.'个;失败'.$fail.'个;'.$message];
    }


}

